//
// OthersTab.cs
// 
// Defines the tab responsible for handling Countries and Genres.
//
// Author:
//   Radu Tofoleanu <kenjiru.ro@gmail.com>
//
// Copyright (C) 2007-2008 Radu Tofoleanu (http://kenjiru.ro)
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
// 
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//

using System;
using System.IO;
using System.Data;
using System.Xml.Serialization;
using Gtk;
using EigaLib;

namespace EigaApp
{
	public partial class OthersTab : Gtk.Bin
	{
		// lucrul cu baza de date
		private EigaDb eigaDb = null;
		
		// variabile
		private TreeIter countryIter;
		private TreeIter genreIter;
		private bool countryAdd = false;
		private bool genreAdd = false;
		
		public EigaDb DbConn
		{
			set {
				eigaDb = value;
			}
		}
		
		public OthersTab()
		{
			Build();
			
			countriesList.AppendColumn("Country name", new CellRendererText(), "text", 1);
			countriesList.Model = new ListStore (typeof(int), typeof(string), typeof(string));
			
			genresList.AppendColumn("Genre name", new CellRendererText (), "text", 1);
			genresList.Model = new ListStore (typeof(int), typeof(string), typeof(string));
		}
		
		public void Refresh()
		{
			ListStore countriesModel = (ListStore) countriesList.Model;
			ListStore genresModel = (ListStore) genresList.Model;
			
			countriesModel.Clear();
			genresModel.Clear();
			
			if(eigaDb.IsConnected() == false) {
				Console.WriteLine ("OthersTab: Refresh(): nu exista conexiune la baza de date!");
				return;
			}
			
			countriesList.Model = eigaDb.GetAllCountries();
			genresList.Model = eigaDb.GetAllGenres();
			
		}
		
		private void Message(string msg)
		{
			Gtk.MessageDialog msgDlg = new Gtk.MessageDialog(null, 
				DialogFlags.DestroyWithParent,
				MessageType.Info,
				ButtonsType.Close, 
				msg);
			msgDlg.Run();
			msgDlg.Destroy();
		}
		
		////////////////////////////////////////////////////////////////////////
		// Countries
		////////////////////////////////////////////////////////////////////////

		private void CountriesBeginEdit()
		{
			if (eigaDb.IsConnected() == false || (countryAdd == false && countryIter.Stamp == 0)) {
				return;
			}
			
			countriesEdit.Sensitive = false;
			countriesSave.Sensitive = true;
			countriesCancel.Sensitive = true;
			countryName.IsEditable = true;
			countryDescription.IsEditable = true;
		}
		
		private void CountriesEndEdit()
		{
			countriesEdit.Sensitive = true;
			countriesSave.Sensitive = false;
			countriesCancel.Sensitive = false;
			countryName.IsEditable = false;
			countryDescription.IsEditable = false;
			
			if (countryAdd == true) {
				countriesAdd.Sensitive = true;
				countryAdd = false;
			}
		}
		
		private void CountriesCancelEdit()
		{
			if (countryAdd == false ) {
				string name = (string) countriesList.Model.GetValue(countryIter, 1);
				string desc = (string) countriesList.Model.GetValue(countryIter, 2);
				
				countryName.Text = name;
				countryDescription.Text = desc;
			} else {
				countryName.Text = "";
				countryDescription.Text = "";
			}
		}
		
		private void CountriesSaveEdit()
		{
			if (countryIter.Stamp == 0)
				return;
			
			string name = countryName.Text;
			string desc = countryDescription.Text;
			int id = (int) countriesList.Model.GetValue(countryIter, 0);
			
			if (! eigaDb.EditCountry (id, name, desc)) {
				CountriesCancelEdit();
				CountriesEndEdit();
				Message ("Nu am putut salva noua inregistrare!");
				return;
			}
			
			CountriesEndEdit();
			
			countriesList.Model.SetValue (countryIter, 1, name);
			countriesList.Model.SetValue (countryIter, 2, desc);
		}
		
		private void CountriesSaveAdd()
		{
			string name = countryName.Text;
			string desc = countryDescription.Text;
			int id = 0;

			if (!eigaDb.AddCountry (out id, name, desc)) {
				Message ("Nu am putut salva noua inregistrare!");
				CountriesCancelEdit();
				CountriesEndEdit();
				return;
			}
			
			CountriesEndEdit();
			
			ListStore model = (ListStore) countriesList.Model;
			model.AppendValues (id, name, desc);
		}
		
		protected virtual void OnCountriesListRowActivated(object o, Gtk.RowActivatedArgs args)
		{
			if (eigaDb.IsConnected() == false) {
				Console.WriteLine ("OthersTab: OnCountriesListRowActivated(): nu exista conexiune la baza de date!");
				return;
			}
			
			TreeView tr = o as TreeView;
			TreePath path = args.Path;
			TreeIter iter;
			
			tr.Model.GetIter (out iter, path);
//			int id = (int) tr.Model.GetValue(iter, 0);
			string name = (string) tr.Model.GetValue(iter, 1);
			string desc = (string) tr.Model.GetValue(iter, 2);
			countryName.Text = name;
			countryDescription.Text = desc;
			countryIter = iter;
		}

		protected virtual void OnCountriesEditClicked(object sender, System.EventArgs e)
		{
			if (eigaDb.IsConnected() == false || !countryAdd && countryIter.Stamp == 0) {
				if (eigaDb.IsConnected() == false)
					Message ("Nu sunteti conectat la baza de date!");
				
				CountriesCancelEdit ();
				CountriesEndEdit ();
				return;
			}
			
			if (countryIter.Stamp == 0)
				return;
				
			CountriesBeginEdit();
		}

		protected virtual void OnCountriesSaveClicked(object sender, System.EventArgs e)
		{
			if (eigaDb.IsConnected() == false || (!countryAdd &&  countryIter.Stamp == 0)) {
				if (eigaDb.IsConnected() == false)
					Message ("Nu sunteti conectat la baza de date!");
				
				CountriesCancelEdit();
				CountriesEndEdit();
				return;
			}
			
			if (countryName.Text == "" || countryDescription.Text == "") {
				Message ("Stringurile nu pot fi nule!");
				return;
			}
			
			if (countryAdd == true) {
				CountriesSaveAdd();
			} else {
				CountriesSaveEdit();
			}
		}

		protected virtual void OnCountriesCancelClicked(object sender, System.EventArgs e)
		{
			CountriesCancelEdit();
			CountriesEndEdit();
		}
		
		protected virtual void OnCountriesAddClicked(object sender, System.EventArgs e)
		{
			if (eigaDb.IsConnected() == false) {
				Message ("Nu sunteti conectat la baza de date!");
				return;
			}
			
			countryAdd = true;
			countriesAdd.Sensitive = false;
			CountriesBeginEdit();
			
			countryName.Text = "";
			countryDescription.Text = "";
		}
		
		protected virtual void OnCountriesDeleteClicked(object sender, System.EventArgs e)
		{
			if (eigaDb.IsConnected() == false) {
				Message ("Nu sunteti conectat la baza de date!");
				return;
			}
			
			TreePath path;
			TreeIter iter;
			TreeViewColumn col;
			ListStore countriesStore = (ListStore) countriesList.Model;
			
			countriesList.GetCursor (out path, out col);
			if (! countriesStore.GetIter (out iter, path)) {
				Message ("Pentru a sterge o tara, trebuie mai intai sa o selectati!");
				return;
			}
			
			int countryID = (int) countriesList.Model.GetValue(iter, 0);
			
			if (!eigaDb.DeleteCountry (countryID)) {
				Message ("Nu am putut sterge tara!");
				return;
			}
			
			countryName.Text = "";
			countryDescription.Text = "";
			
			if (iter.Stamp == countryIter.Stamp)
				countryIter.Stamp = 0;
			
			countriesStore.Remove (ref iter);
		}
		
		////////////////////////////////////////////////////////////////////////
		// Genres
		////////////////////////////////////////////////////////////////////////
		
		private void GenresBeginEdit()
		{
			if (eigaDb.IsConnected() == false || (genreAdd == false && genreIter.Stamp == 0)) {
				return;
			}
			
			genresEdit.Sensitive = false;
			genresSave.Sensitive = true;
			genresCancel.Sensitive = true;
			genreName.IsEditable = true;
			genreDescription.IsEditable = true;
		}
		
		private void GenresEndEdit()
		{
			genresEdit.Sensitive = true;
			genresSave.Sensitive = false;
			genresCancel.Sensitive = false;
			genreName.IsEditable = false;
			genreDescription.IsEditable = false;
			
			if (genreAdd == true) {
				genresAdd.Sensitive = true;
				genreAdd = false;
			}
		}
		
		private void GenresCancelEdit()
		{
			if (genreAdd == false) {
				string name = (string) genresList.Model.GetValue(genreIter, 1);
				string desc = (string) genresList.Model.GetValue(genreIter, 2);

				genreName.Text = name;
				genreDescription.Text = desc;
			}
		}

		private void GenresSaveEdit()
		{
			if (genreIter.Stamp == 0)
				return;
			
			int id = (int) genresList.Model.GetValue(genreIter, 0);
			
			string name = genreName.Text;
			string desc = genreDescription.Text;
			
			if (! eigaDb.EditCountry (id, name, desc)) {
				GenresCancelEdit();
				GenresEndEdit();
				Message ("Nu am putut salva noua inregistrare!");
				return;
			}
			
			GenresEndEdit();
			
			genresList.Model.SetValue (genreIter, 1, name);
			genresList.Model.SetValue (genreIter, 2, desc);
		}
		
		private void GenresSaveAdd()
		{
			string name = genreName.Text;
			string desc = genreDescription.Text;
			int id = 0;
			
			if (!eigaDb.AddGenre (out id, name, desc)) {
				Message ("Nu am putut salva noua inregistrare!");
				GenresCancelEdit();
				GenresEndEdit();
				return;
			}
			
			GenresEndEdit();
			
			ListStore model = (ListStore) genresList.Model;
			model.AppendValues (id, name, desc);
		}
		
		protected virtual void OnGenresListRowActivated(object o, Gtk.RowActivatedArgs args)
		{
			if (eigaDb.IsConnected() == false) {
				Message ("Nu sunteti conectat la baza de date!");
				return;
			}
			
			TreeView tr = o as TreeView;
			TreePath path = args.Path;
			TreeIter iter;
			
			tr.Model.GetIter (out iter, path);
//			int id = (int) tr.Model.GetValue(iter, 0);
			string name = (string) tr.Model.GetValue(iter, 1);
			string desc = (string) tr.Model.GetValue(iter, 2);
			genreName.Text = name;
			genreDescription.Text = desc;
			genreIter = iter;
		}

		protected virtual void OnGenresEditClicked(object sender, System.EventArgs e)
		{
			GenresBeginEdit();
		}

		protected virtual void OnGenresSaveClicked(object sender, System.EventArgs e)
		{
			if (eigaDb.IsConnected() == false || (!genreAdd && genreIter.Stamp == 0)) {
				if (eigaDb.IsConnected() == false)
					Message ("Nu sunteti conectat la baza de date!");
				
				GenresCancelEdit ();
				GenresEndEdit ();
				return;
			}
			
			if (genreName.Text == "" || genreDescription.Text == "") {
				Message ("Introduceti toate datele!");
				return;
			}
			
			if (genreAdd == true) {
				GenresSaveAdd();
			} else {
				GenresSaveEdit();
			}
		}

		protected virtual void OnGenresCancelClicked(object sender, System.EventArgs e)
		{
			GenresCancelEdit();
			GenresEndEdit();
		}

		protected virtual void OnGenresAddClicked(object sender, System.EventArgs e)
		{
			if (eigaDb.IsConnected() == false) {
				Message ("Nu sunteti conectat la baza de date!");
				return;
			}
			
			genreAdd = true;
			genresAdd.Sensitive = false;
			GenresBeginEdit();
			
			genreName.Text = "";
			genreDescription.Text = "";
		}

		protected virtual void OnGenresDeleteClicked(object sender, System.EventArgs e)
		{
			if (eigaDb.IsConnected() == false) {
				Message ("Nu sunteti conectat la baza de date!");
				return;
			}
			
			TreePath path;
			TreeIter iter;
			TreeViewColumn col;
			ListStore genresStore = (ListStore) genresList.Model;
			
			genresList.GetCursor (out path, out col);
			if (! genresStore.GetIter (out iter, path)) {
				Message ("Pentru a sterge o persoana, trebuie mai intai sa o selectati!");
				return;
			}
			
			int genreID = (int) genresList.Model.GetValue(iter, 0);
			
			if (!eigaDb.DeleteGenre(genreID)) {
				Message ("Nu am putut sterge inregistrare!");
				return;
			}
			
			genreName.Text = "";
			genreDescription.Text = "";

			genresStore.Remove (ref iter);
			if (iter.Stamp == genreIter.Stamp)
				genreIter.Stamp = 0;
		}
	}
}
